package cn.huobi.order.service.impl;

import cn.huobi.basic.config.BussinessException;
import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.order.domain.OrderPetAcquisition;
import cn.huobi.order.dto.OrderConfirmDto;
import cn.huobi.order.mapper.OrderPetAcquisitionMapper;
import cn.huobi.order.service.IOrderPetAcquisitionService;
import cn.huobi.pet.domain.Pet;
import cn.huobi.pet.domain.RejectSearchMasterLog;
import cn.huobi.pet.domain.SearchMasterMsg;
import cn.huobi.pet.service.IPetService;
import cn.huobi.pet.service.IRejectSearchMasterLogService;
import cn.huobi.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huobi
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IPetService petService;
    @Autowired
    private IRejectSearchMasterLogService rejectSearchMasterLogService;

    /**
     * 交易成功
     */
    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 根据id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderConfirmDto.getId());
        // 判断订单是否存在
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 查询寻主消息修改状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);
        // 生成动物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petService.add(pet);

        // 修改订单状态
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    /**
     * 交易失败
     */
    @Override
    public void cancel(Long orderId) {
        // 根据id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        // 判断订单是否存在
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 修改订单状态
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 查询寻主消息修改状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);
        // todo 保存取消订单日志

        RejectSearchMasterLog rejectSearchMasterLog = new RejectSearchMasterLog();
        rejectSearchMasterLog.setNote("有内鬼，取消交易！");
        rejectSearchMasterLog.setMsgId(orderPetAcquisition.getSearchMasterMsgId());
        rejectSearchMasterLog.setEmpId(orderPetAcquisition.getEmpId());
        rejectSearchMasterLogService.add(rejectSearchMasterLog);

    }
}
